<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field="name">类型名称</th>
							<!-- 							<th class="text-center" data-field="code">唯一编号</th> -->
							<th class="text-center" data-field="status">状态</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">添加</h4>
				</div>
				<div class="modal-body">
					<div class="input-group">
						<span class="input-group-addon">类型名称：</span> <input type="text" class="form-control" id="dom1" value="" aria-describedby="basic-addon1">
					</div>
					<!-- 					<br> -->
					<!-- 					<div class="input-group"> -->
					<!-- 						<span class="input-group-addon">唯一编号：</span> <input type="text" class="form-control" id="dom2" value="" aria-describedby="basic-addon1"> -->
					<!-- 					</div> -->
				</div>
				<div class="modal-footer">
					<button id="submitBtn" type="button" class="btn btn-primary" onclick="adopt();">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	function switchFormatter(value, row, index) {
		if (row.status == 0) {
			return [ "<div  class='switch switch-mini'><input id='"+row.id+"' name='switch-animate' type='checkbox' /></div>" ].join('');
		} else {
			return [ "<div class='switch switch-mini'><input id='"+row.id+"' name='switch-animate' type='checkbox'  checked/></div>" ].join('');
		}

	}
	function actionFormatter(value, row, index) {
		return "<button class='btn btn-danger btn-xs like' onclick='del(\"" + row.id + "\");'>删除</button>";
	}

	var table_dataset = [ {
		field : 'name',
		align : 'center',
	}, {
		field : 'status',
		align : 'center',
		formatter : switchFormatter
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/selectPropertyTypes",
		idField : "sid",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
		onLoadSuccess : function(data) {
			//console.log(data);
			$('[name="switch-animate"]').bootstrapSwitch({
				onText : "启用",
				offText : "禁用",
				onColor : "info",
				offColor : "danger",
				size : "mini",
				onSwitchChange : function(event, sta) {
					//var sid=$(event).parent().prevObject["0"].currentTarget.id;
					var id = event.currentTarget.id;
					var status = 0;
					if (sta) {
						status = 1
					}
					/* console.log(event);
					console.log(event.currentTarget.id); */

					$.ajax({
						url : "${pageContext.request.contextPath}/updatePropertyType",
						type : "post",
						data : {
							id : id,
							status : status
						},
						dataType : "json",
						success : function(data) {
							var result = data['result'];
							if (result === 1) {
								/* layer.msg('操作成功!', {
									icon : 1,
									time : 800
								}); */
							} else {
								/* layer.msg('操作失败!', {
									icon : 2,
									time : 800
								}); */
								$("#" + sid + "").bootstrapSwitch('status', !status, true);

							}
						},
						error : function() {
							layer.msg('系统繁忙，请稍后再试!', {
								icon : 2,
								time : 800
							});
							$("#" + sid + "").bootstrapSwitch('state', !state, true);
						}
					});

				}
			});
		}
	});

	function showModal() {
		$("#dom1").val('');
		// 		$("#dom2").val('');
		$('#modal').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function adopt() {
		$("#submitBtn").button('loading');
		var name = $("#dom1").val();
		// 		var code = $("#dom2").val();
		if (name === '') {
			layer.alert("新增的数据中填写不完整!");
			$("#submitBtn").button('reset');
			return false;
		}
		$.ajax({
			url : "${pageContext.request.contextPath}/insertPropertyType",
			type : "post",
			data : {
				id : '',
				name : name
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
					selectType();
					$("#submitBtn").button('reset');
				} else if (result === 2) {
					layer.msg('唯一编号重复!', {
						icon : 2,
						time : 800
					});
					$("#submitBtn").button('reset');
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
					$("#submitBtn").button('reset');
				}
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
				$("#submitBtn").button('reset');
			}
		});
	}

	function del(id) {
		layer.confirm("是否删除此项？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/updatePropertyType",
				type : "post",
				data : {
					id : id,
					status : '-1'
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
</script>
</html>
